package com.nbjtjc.safe.service.danger;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.danger.*;
import com.nbjtjc.safe.model.danger.DangerDistribute;
import com.nbjtjc.safe.model.danger.DangerInspect;
import com.nbjtjc.safe.model.danger.DangerInspectListPublic;
import com.nbjtjc.safe.model.danger.DangerInspectPublic;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @auther guqianbin
 * @date 2018/4/10 10:53
 **/
@Service
public class DangerDistributeService {
    private static Logger logger= LoggerFactory.getLogger(DangerDistributeService.class);
    @Autowired
    private DangerDistributeMapper dangerDistributeMapper;
    @Autowired
    private DangerDistributeTargetMapper dangerDistributeTargetMapper;

    @Autowired
    private DangerInspectPublicMapper dangerInspectPublicMapper;
    @Autowired
    private DangerInspectListPublicMapper dangerInspectListPublicMapper;

    @Autowired
    private DangerInspectMapper dangerInspectMapper;
    @Autowired
    private DangerInspectListMapper dangerInspectListMapper;

    public Result add(DangerDistribute dangerDistribute) {
        Result result = new Result();
        if (ObjectUtil.isNull(dangerDistribute)) {
            result.setFalse("不能为空");

            return result;
        }
        dangerDistribute.setCreateUserId(MyShiroUtils.getSessionUserId());
        dangerDistribute.setDeptId(MyShiroUtils.getDeptId());
        dangerDistributeMapper.insertSelective(dangerDistribute);
        addDangerDistributeTargets(dangerDistribute);
        addInspect(dangerDistribute);
        return result;
    }

    public Result delete(DangerDistribute dangerDistribute) {
        Result result = new Result();
        dangerDistributeMapper.deleteByPrimaryKey(dangerDistribute);
        return result;
    }

    public Result update(DangerDistribute dangerDistribute) {
        Result result = new Result();
        dangerDistributeMapper.updateByPrimaryKeySelective(dangerDistribute);
        return result;
    }

    public Result find(DangerDistribute dangerDistribute) {
        Result result = new Result();
        PageHelper.startPage(dangerDistribute.getPageCurrent(),dangerDistribute.getPageSize());
        List<DangerDistribute> dangerDistributes=dangerDistributeMapper.search(dangerDistribute);
        if(CollectionUtil.isNotEmpty(dangerDistributes)){
            result.setData(new PageInfo<>(dangerDistributes));
        }else {
            logger.info("dangerDistribute  查找为空"+dangerDistribute.toString());
            result.setData(new PageInfo<>());
        }
        return result;
    }

    /**
     * 添加对应的派单的对象记录
     * @param dangerDistribute
     */
    private void addDangerDistributeTargets(DangerDistribute dangerDistribute){
        if (CollectionUtil.isNotEmpty(dangerDistribute.getDangerDistributeTargets())) {
            dangerDistribute.getDangerDistributeTargets().forEach(dangerDistributeTarget -> {
                dangerDistributeTarget.setDistributeId(dangerDistribute.getId());
            });
            dangerDistributeTargetMapper.insertList(dangerDistribute.getDangerDistributeTargets());
        }
    }

    /**
     * 增加派单对应的检查单
     * @param dangerDistribute
     * @return
     */
    private DangerInspectPublic addInspect(DangerDistribute dangerDistribute){
        DangerInspectPublic dangerInspectPublic=null;
        if(ObjectUtil.isNull(dangerDistribute.getDangerInspect())){
            return null;
        }
        DangerInspect dangerInspect=dangerInspectMapper.selectByPrimaryKey(dangerDistribute.getDangerInspect());
        dangerInspectPublic=addInspectPublic(new DangerInspectPublic(),dangerInspect,dangerDistribute);
        if (CollectionUtil.isNotEmpty(dangerDistribute.getDangerInspectListPublics())) {
           addInspectListPublic(dangerDistribute,dangerInspectPublic);
        }
        return dangerInspectPublic;
    }

    /**
     * 插入
     * @param dangerInspectPublic
     * @param dangerInspect
     * @return
     */
    private DangerInspectPublic addInspectPublic(DangerInspectPublic dangerInspectPublic
            ,DangerInspect dangerInspect,DangerDistribute dangerDistribute){
        dangerInspectPublic.setCreateTime(new Date());
        dangerInspectPublic.setCreateUserId(MyShiroUtils.getSessionUserId());
        dangerInspectPublic.setPublicDeptId(MyShiroUtils.getDeptId());
        dangerInspectPublic.setInspectId(dangerInspect.getId());
        dangerInspectPublic.setDistributeId(dangerDistribute.getId());
        dangerInspectPublicMapper.insertSelective(dangerInspectPublic);
        return dangerInspectPublic;
    }

    private void addInspectListPublic(DangerDistribute dangerDistribute,
                                      DangerInspectPublic dangerInspectPublic){
        dangerDistribute.getDangerInspectListPublics().forEach(dil->{
            DangerInspectListPublic dangerInspectListPublic=new DangerInspectListPublic();
            dangerInspectListPublic.setInspectContent(dil.getInspectContent());
            dangerInspectListPublic.setInspectProject(dil.getInspectProject());
            dangerInspectListPublic.setInspectPublicId(dangerInspectPublic.getId());
            dangerInspectListPublic.setNum(dil.getNum());
        });
        dangerInspectListPublicMapper.insertList(dangerDistribute.getDangerInspectListPublics());
    }
}
